﻿using Engine.Input;
using Engine;
using Game;
using GameEntitySystem;
using TemplatesDatabase;


namespace SCIENEW
{
	public class ComponentNGui : ComponentGui, IUpdateable
	{
		public ComponentGui ComponentGui;
        public BitmapButtonWidget m_techTreeButton;

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
            base.Load(valuesDictionary, idToEntityMap);
            m_techTreeButton = m_componentPlayer.GuiWidget.Children.Find<BitmapButtonWidget>("TechTreeButton", false);
            if (m_techTreeButton == null) {
                m_techTreeButton = new() { Name = "TechTreeButton", NormalSubtexture = ContentManager.Get<Subtexture>("Textures/Gui/TechTreeButton1"), ClickedSubtexture = ContentManager.Get<Subtexture>("Textures/Gui/TechTreeButton2"), Size = new Vector2(68, 64), Margin = new Vector2(4, 0)};
                m_componentPlayer.GuiWidget.Children.Find<StackPanelWidget>("MoreContents").Children.Add(m_techTreeButton);
            }
        }

        public new void Update(float dt)
		{
			HandleInput();
			base.UpdateWidgets();
		}


		public static Widget OpenEntity(IInventory inventory, Entity entity)
		{
			var name = entity.ValuesDictionary.Keys.ToArray()[0].ToString();
			switch (name)
			{
				case "SteamBoat":
					if (entity.FindComponent<ComponentSteamEngineV>() != null)
						return new SteamBoatWidget(inventory, entity.FindComponent<ComponentSteamEngineV>());
					break;
				case "Train":
					if (entity.FindComponent<ComponentSteamEngineV>() != null)
						return new SteamTrainWidget(inventory, entity.FindComponent<ComponentSteamEngineV>());
					break;
				case "Carriage":
					if (entity.FindComponent<ComponentChest>() != null)
						return new ChestNameWidget(inventory, entity.FindComponent<ComponentChest>(),"车厢");
					break;
			}
					
			return null;
		}

	



	public new void HandleInput()
		{
			WidgetInput input = m_componentPlayer.GameWidget.Input;
			PlayerInput playerInput = m_componentPlayer.ComponentInput.PlayerInput;
			ComponentRider componentRider = m_componentPlayer.ComponentRider;
			if (m_componentPlayer.GameWidget.ActiveCamera.IsEntityControlEnabled)
			{
				if (!m_keyboardHelpMessageShown && (m_componentPlayer.PlayerData.InputDevice & WidgetInputDevice.Keyboard) != 0 && Time.PeriodicEvent(7.0, 0.0))
				{
					m_keyboardHelpMessageShown = true;
					DisplaySmallMessage(LanguageControl.Get(fName, 1), Color.White, blinking: true, playNotificationSound: true);
				}
				else if (!m_gamepadHelpMessageShown && (m_componentPlayer.PlayerData.InputDevice & WidgetInputDevice.Gamepads) != 0 && Time.PeriodicEvent(7.0, 0.0))
				{
					m_gamepadHelpMessageShown = true;
					DisplaySmallMessage(LanguageControl.Get(fName, 2), Color.White, blinking: true, playNotificationSound: true);
				}
			}
			if (playerInput.KeyboardHelp)
			{
				if (m_keyboardHelpDialog == null)
				{
					m_keyboardHelpDialog = new KeyboardHelpDialog();
				}
				if (m_keyboardHelpDialog.ParentWidget != null)
				{
					DialogsManager.HideDialog(m_keyboardHelpDialog);
				}
				else
				{
					DialogsManager.ShowDialog(m_componentPlayer.GuiWidget, m_keyboardHelpDialog);
				}
			}
			if (playerInput.GamepadHelp)
			{
				if (m_gamepadHelpDialog == null)
				{
					m_gamepadHelpDialog = new GamepadHelpDialog();
				}
				if (m_gamepadHelpDialog.ParentWidget != null)
				{
					DialogsManager.HideDialog(m_gamepadHelpDialog);
				}
				else
				{
					DialogsManager.ShowDialog(m_componentPlayer.GuiWidget, m_gamepadHelpDialog);
				}
			}
			if (m_helpButtonWidget.IsClicked)
			{
				ScreensManager.SwitchScreen("Help");
			}
			if (playerInput.ToggleInventory || m_inventoryButtonWidget.IsClicked)
			{
				if (componentRider.Mount != null && ModalPanelWidget == null)
				{
					Widget widget = OpenEntity(m_componentPlayer.ComponentMiner.Inventory, m_componentPlayer.ComponentRider.Mount.Entity);
					if (widget != null)
					{
						ModalPanelWidget = widget;
						AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
						return;
					}
				}
				if (IsInventoryVisible())
				{
					ModalPanelWidget = null;
				}
				else
				{
					ModalPanelWidget = ((m_componentPlayer.ComponentMiner.Inventory is ComponentCreativeInventory) ? ((CanvasWidget)new CreativeInventoryWidget(m_componentPlayer.Entity)) : ((CanvasWidget)new FullInventoryWidget(m_componentPlayer.ComponentMiner.Inventory, m_componentPlayer.Entity.FindComponent<ComponentCraftingTable>(throwOnError: true))));
				}
			}
			if (playerInput.ToggleClothing || m_clothingButtonWidget.IsClicked)
			{
				if (IsClothingVisible())
				{
					ModalPanelWidget = null;
				}
				else
				{
					ClothingWidget clothingWidget = new ClothingWidget(m_componentPlayer);
					ModsManager.HookAction("ClothingWidgetOpen", delegate (ModLoader modLoader)
					{
						modLoader.ClothingWidgetOpen(this, clothingWidget);
						return false;
					});
					ModalPanelWidget = clothingWidget;
				}
			}
			if (m_sneakButtonWidget.IsClicked || playerInput.ToggleSneak)
			{
				float targetCrouchFactor = m_componentPlayer.ComponentBody.TargetCrouchFactor;
				m_componentPlayer.ComponentBody.TargetCrouchFactor = ((targetCrouchFactor == 0f) ? 1 : 0);
				if (m_componentPlayer.ComponentBody.TargetCrouchFactor != targetCrouchFactor)
				{
					if (m_componentPlayer.ComponentBody.TargetCrouchFactor > 0f)
					{
						DisplaySmallMessage(LanguageControl.Get(fName, 3), Color.White, blinking: false, playNotificationSound: false);
					}
					else
					{
						DisplaySmallMessage(LanguageControl.Get(fName, 4), Color.White, blinking: false, playNotificationSound: false);
					}
				}
			}
			if (componentRider != null && (m_mountButtonWidget.IsClicked || playerInput.ToggleMount))
			{
				bool flag = componentRider.Mount != null;
				if (flag)
				{
					componentRider.StartDismounting();
				}
				else
				{
					ComponentMount componentMount = componentRider.FindNearestMount();
					if (componentMount != null)
					{
						componentRider.StartMounting(componentMount);
					}
				}
				if (componentRider.Mount != null != flag)
				{
					if (componentRider.Mount != null)
					{
						DisplaySmallMessage(LanguageControl.Get(fName, 5), Color.White, blinking: false, playNotificationSound: false);
					}
					else
					{
						DisplaySmallMessage(LanguageControl.Get(fName, 6), Color.White, blinking: false, playNotificationSound: false);
					}
				}
			}
			if ((m_editItemButton.IsClicked || playerInput.EditItem) && m_componentPlayer.ComponentBlockHighlight.NearbyEditableCell.HasValue)
			{
				Point3 value = m_componentPlayer.ComponentBlockHighlight.NearbyEditableCell.Value;
				int cellValue = m_subsystemTerrain.Terrain.GetCellValue(value.X, value.Y, value.Z);
				SubsystemBlockBehavior[] blockBehaviors = m_subsystemBlockBehaviors.GetBlockBehaviors(Terrain.ExtractContents(cellValue));
				for (int i = 0; i < blockBehaviors.Length && !blockBehaviors[i].OnEditBlock(value.X, value.Y, value.Z, cellValue, m_componentPlayer); i++)
				{
				}
			}
			else if ((m_editItemButton.IsClicked || playerInput.EditItem) && IsActiveSlotEditable())
			{
				IInventory inventory = m_componentPlayer.ComponentMiner.Inventory;
				if (inventory != null)
				{
					int activeSlotIndex = inventory.ActiveSlotIndex;
					int slotValue = inventory.GetSlotValue(activeSlotIndex);
					int num = Terrain.ExtractContents(slotValue);
					if (BlocksManager.Blocks[num].IsEditable_(slotValue))
					{
						SubsystemBlockBehavior[] blockBehaviors2 = m_subsystemBlockBehaviors.GetBlockBehaviors(Terrain.ExtractContents(num));
						for (int j = 0; j < blockBehaviors2.Length && !blockBehaviors2[j].OnEditInventoryItem(inventory, activeSlotIndex, m_componentPlayer); j++)
						{
						}
					}
				}
			}
			if (m_subsystemGameInfo.WorldSettings.GameMode == GameMode.Creative && (m_creativeFlyButtonWidget.IsClicked || playerInput.ToggleCreativeFly) && componentRider.Mount == null)
			{
				bool isCreativeFlyEnabled = m_componentPlayer.ComponentLocomotion.IsCreativeFlyEnabled;
				m_componentPlayer.ComponentLocomotion.IsCreativeFlyEnabled = !isCreativeFlyEnabled;
				if (m_componentPlayer.ComponentLocomotion.IsCreativeFlyEnabled != isCreativeFlyEnabled)
				{
					if (m_componentPlayer.ComponentLocomotion.IsCreativeFlyEnabled)
					{
						m_componentPlayer.ComponentLocomotion.JumpOrder = 1f;
						DisplaySmallMessage(LanguageControl.Get(fName, 7), Color.White, blinking: false, playNotificationSound: false);
					}
					else
					{
						DisplaySmallMessage(LanguageControl.Get(fName, 8), Color.White, blinking: false, playNotificationSound: false);
					}
				}
			}
			if (m_cameraButtonWidget.IsClicked || playerInput.SwitchCameraMode || input.IsPadButtonDownOnce(GamePadButton.RightThumb) || input.IsPadButtonDownOnce(GamePadButton.DPadDown))
			{
				ModsManager.HookAction("OnCameraChange", delegate (ModLoader modLoader)
				{
					modLoader.OnCameraChange(m_componentPlayer, this);
					return false;
				});
			}
			if (m_photoButtonWidget.IsClicked || playerInput.TakeScreenshot)
			{
				ScreenCaptureManager.CapturePhoto(delegate
				{
					DisplaySmallMessage(LanguageControl.Get(fName, 13), Color.White, blinking: false, playNotificationSound: false);
				}, delegate
				{
					DisplaySmallMessage(LanguageControl.Get(fName, 14), Color.White, blinking: false, playNotificationSound: false);
				});
			}
			if (m_subsystemGameInfo.WorldSettings.GameMode == GameMode.Creative && (m_lightningButtonWidget.IsClicked || playerInput.Lighting))
			{
				Matrix matrix = Matrix.CreateFromQuaternion(m_componentPlayer.ComponentCreatureModel.EyeRotation);
				base.Project.FindSubsystem<SubsystemWeather>(throwOnError: true).ManualLightingStrike(m_componentPlayer.ComponentCreatureModel.EyePosition, matrix.Forward);
			}
			if (m_subsystemGameInfo.WorldSettings.GameMode == GameMode.Creative && (m_timeOfDayButtonWidget.IsClicked || playerInput.TimeOfDay))
			{
				float num2 = MathUtils.Remainder(0.25f, 1f);
				float num3 = MathUtils.Remainder(0.5f, 1f);
				float num4 = MathUtils.Remainder(0.75f, 1f);
				float num5 = MathUtils.Remainder(1f, 1f);
				float num6 = MathUtils.Remainder(num2 - m_subsystemTimeOfDay.TimeOfDay, 1f);
				float num7 = MathUtils.Remainder(num3 - m_subsystemTimeOfDay.TimeOfDay, 1f);
				float num8 = MathUtils.Remainder(num4 - m_subsystemTimeOfDay.TimeOfDay, 1f);
				float num9 = MathUtils.Remainder(num5 - m_subsystemTimeOfDay.TimeOfDay, 1f);
				float num10 = MathUtils.Min(num6, num7, num8, num9);
				if (num6 == num10)
				{
					m_subsystemTimeOfDay.TimeOfDayOffset += num6;
					DisplaySmallMessage(LanguageControl.Get(fName, 15), Color.White, blinking: false, playNotificationSound: false);
				}
				else if (num7 == num10)
				{
					m_subsystemTimeOfDay.TimeOfDayOffset += num7;
					DisplaySmallMessage(LanguageControl.Get(fName, 16), Color.White, blinking: false, playNotificationSound: false);
				}
				else if (num8 == num10)
				{
					m_subsystemTimeOfDay.TimeOfDayOffset += num8;
					DisplaySmallMessage(LanguageControl.Get(fName, 17), Color.White, blinking: false, playNotificationSound: false);
				}
				else if (num9 == num10)
				{
					m_subsystemTimeOfDay.TimeOfDayOffset += num9;
					DisplaySmallMessage(LanguageControl.Get(fName, 18), Color.White, blinking: false, playNotificationSound: false);
				}
			}
			if (ModalPanelWidget != null)
			{
				if (input.Cancel || input.Back || m_backButtonWidget.IsClicked)
				{
					ModalPanelWidget = null;
				}
			}
            if (m_techTreeButton.IsClicked) {
                Project.FindSubsystem<SubsystemTechTree>(true).Show();
            }
			else if (input.Back || m_backButtonWidget.IsClicked)
			{
				DialogsManager.ShowDialog(m_componentPlayer.GuiWidget, new GameMenuDialog(m_componentPlayer));
			}
		}

	}
}
